const express=require("express")
const router=express.Router();
const pool=require("../pool");
//商品搜索
router.get("/",(req,res)=>{
  //1:获取参数 key pno pageSize type
  var key = req.query.key;
  //var pno = req.query.pno;
  //console.log(key);
  //2：设置默认值 1 7
 // var pageSize = req.query.pageSize;
 // if(!pno){pno = 1};
 // if(!pageSize){pageSize = 7};
  
  var searchData={
    carData:{} ,
    articleData:{}
  };
  new Promise((resolve,reject)=>{
    //3：创建sql搜索商品信息
    var sql =" SELECT title,des,price,src,cid";
        sql+=" FROM car_type";
        sql+=" WHERE title LIKE ?";
        //sql+=" LIMIT ?,?";
      //4：offset pageSize parseInt()
    //var offset = (pno-1) * pageSize;
    //pageSize = parseInt(pageSize);
    //5:发送sql
    pool.query(sql,["%"+key+"%"],(err,result)=>{
      if(err) throw err;
      //res.send({code:1,data:result})
      if(result.length > 0){
        searchData.carData.code = 0;
        searchData.carData.data = result;
      }else{
        searchData.carData.code = null;
        searchData.carData.msg = '没有找到内容'
      }
      resolve(true);
    })
  }).then(value=>{
      return new Promise((resolve,reject)=>{
        var sql = " SELECT did,title,comments";
            sql+=" dates,pic";
            sql+=" FROM yhc_discover_all";
            sql+=" WHERE title LIKE ?";
            //sql+=" LIMIT ?,?";
          //4：offset pageSize parseInt()
        //var offset = (pno-1) * pageSize;
       // pageSize = parseInt(pageSize);
        //5:发送sql
        pool.query(sql,["%"+key+"%"],(err,result)=>{
          if(err) throw err;
          //console.log(key);
          if(result.length > 0){
            searchData.articleData.code = 1;
            searchData.articleData.data = result;
            console.log(result);
          }else{
            searchData.articleData.code = null;
            searchData.articleData.msg = '没有找到内容'
          }
          res.send(searchData);
          res.end();
        })
    })
  })
})
module.exports=router;